package com.foreveross.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.SYWebService.SYHttpclientCall;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.common.Constants;
import com.foreveross.dao.HrHouseInfoDAO;
import com.foreveross.util.ObjectToMap;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.Tenement;

@Service("hrHouseInfoService")
@Transactional
public class HrHouseInfoService {

	@Resource
	HrHouseInfoDAO hrHouseInfoDao;

	public <T> void saveOrUpdate(T entity, String type) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			hrHouseInfoDao.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			hrHouseInfoDao.updateObject(entity);
		}

	}

	/**
	 * 同步楼盘项目ID
	 * 
	 * @param resultVo
	 * @param conditionVo
	 * @param projectName
	 * @return
	 */
	public DataVo synHrHouses(ConditionVo conditionVo,
			String projectName) throws Exception {
		DataVo dataVo = null;
		Map<String, String> params = new HashMap<String, String>();
		params = ObjectToMap.objectToMapViaFields(conditionVo, true);
		SYResult sysResult = SYHttpclientCall.getSYData(params);
		List<SYSWin> list = sysResult.getUserRev_GetProject();
		if (list != null && !list.isEmpty()) {
			SYSWin sYSWin = list.get(0);
			List<DataVo> listv = sYSWin.getSyswin();
			if (listv != null && !listv.isEmpty() && projectName != null
					&& !"".equals(projectName)) {
				for (int i = 0; i < listv.size(); i++) {
					DataVo datavo = listv.get(i);
					if (datavo.getProjectName().equals(projectName)) {
						dataVo = datavo;
						break;
					}
				}
			}
		}
		return dataVo;
	}

	public Map<String, Object> getCityHouseList(String province)
			throws Exception {
		List<HrHouses> list = hrHouseInfoDao.getCityHouseList(province);
		
		List<Map<String, Object>> listv = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (HrHouses hc : list) {
			Map<String, Object> mapO = new HashMap<String, Object>();
			mapO.put("chooseItem", hc.getName());
			mapO.put("chooseValue", hc.getId());
			mapO.put("projectId", hc.getProjectId());
			listv.add(mapO);
		}
		map.put("data", listv);
		map.put("total", listv.size());
		
		return map;
	}
	
	public long getHouseListCount(String name, String proType, String proLine,
			String userId) throws Exception {
		return hrHouseInfoDao
				.findHouseListCount(name, proType, proLine, userId);
	}

	public List<HrHouses> getHouseList(String name, String proType,
			String proLine, int pageIndex, int pageSize, String userId)
			throws Exception {
		return hrHouseInfoDao.findHouseList(name, proType, proLine, pageIndex,
				pageSize, userId);
	}

	public void setTenementHouse(Tenement t, String id) throws Exception {
		hrHouseInfoDao.setTenementHouse(t, id);
	}

	public void deleteHouses(String ids) throws Exception {
		for (String id : ids.split(",")) {
			hrHouseInfoDao.deleteHouse(id);
		}
	}

	public void editIsDomicile(String weixinId, String userId) throws Exception {
		hrHouseInfoDao.editIsDomicile(weixinId, userId);
	}

	public List<HrHouses> getHrHousesSelect(String id, String cityId, String userId)
			throws Exception {
		List<HrHouses> list = hrHouseInfoDao.findHouseList(cityId, userId);
		for (int i = 0; i < list.size(); i++) {
			HrHouses h = (HrHouses) list.get(i);
			if (h.getId().equals(id))
				h.setSelected(true);
		}
		return list;
	}
	
	public List<HrHouses> getHousesListWithOutTenementSelect(String id, String cityId, String userId)
			throws Exception {
		List<HrHouses> list = hrHouseInfoDao.findHousesListWithOutTenement(cityId, userId);
		for (HrHouses h : list) {
			if (h.getId().equals(id)) h.setSelected(true);
		}
		return list;
	}

	public void updateHrHousesProjectId(String hrHousesId, String projectId)
			throws Exception {
		hrHouseInfoDao.updateHrHousesProjectId(hrHousesId, projectId);
	}


}
